package com.easychat.controller;

import com.easychat.annotation.GlobalInterceptor;
import com.easychat.common.Result;
import com.easychat.dto.SysSettingDto;
import com.easychat.entity.config.AppConfig;
import com.easychat.entity.constants.Constants;
import com.easychat.redis.RedisComponent;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;

/**
 *
 *
 * @author makejava
 * @since 2024-10-01 17:32:20
 */
@RestController("adminSettingController")
@RequestMapping("/admin")
public class AdminSettingController {
    /**
     * 服务对象
     */
    @Resource
    private RedisComponent redisComponent;
    @Resource
    private AppConfig appConfig;

    @GetMapping("/getSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public Result getSysSetting(){
        SysSettingDto sysSettingDto = redisComponent.getSysSetting();
        return Result.ok(sysSettingDto);
    }

    @PostMapping("/saveSysSetting")
    @GlobalInterceptor(checkAdmin = true)
    public Result saveSysSetting(SysSettingDto sysSettingDto,
                                 MultipartFile robotFile,
                                 MultipartFile robotCover) throws IOException {

        if(robotFile != null){
            String baseFolder = appConfig.getProjectFolder() + Constants.FILE_FOLDER_FILE;
            File targetFileFolder = new File(baseFolder + Constants.FILE_FOLDER_AVATAR_NAME);
            if(targetFileFolder.exists()){
                targetFileFolder.mkdirs();
            }
            String filePath = targetFileFolder.getPath() + "/" + Constants.ROBOT_UID + Constants.IMAGE_SUFFIX;
            robotFile.transferTo(new File(filePath));
            robotCover.transferTo(new File(filePath + Constants.COVER_IMAGE_SUFFIX));
        }
        redisComponent.saveSysSetting(sysSettingDto);
        return Result.ok();
    }

}

